package com.nefu.editor.service.Impl;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.common.util.TokenUtil;
import com.nefu.editor.core.mapper.UserMapper;
import com.nefu.editor.core.model.User;
import com.nefu.editor.core.model.Vo.UserVo;
import com.nefu.editor.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-12
 * @since : java 8
 */
@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Map<String, Object> postLogin(User user) throws EdiException {
        Map<String, Object> rtv = null;
        List<User> users = userMapper.selectUserByCondition(user);
        if (null != users && 1 == users.size()) {
            user = users.get(0);
            user.setToken(TokenUtil.getToken());
            if (0 < userMapper.updateTokenBySystemId(user)) {
                rtv = new HashMap<>(3);
                user = users.get(0);
                rtv.put("systemId", user.getSystemId());
                rtv.put("token", user.getToken());
                rtv.put("type", user.getType());
            } else {
                throw new EdiException("登录失败!");
            }
        } else {
            throw new EdiException("用户名或密码不正确!");
        }
        return rtv;
    }

    @Override
    public boolean changePassword(UserVo userVo) throws EdiException {
        boolean rtv = false;
        List<User> users = userMapper.selectByUserSystemId(userVo);
        if (null != users && 0 != users.size()) {
            User user = users.get(0);
            logger.info("userPassword=" + user.getPassword() + " username=" + user.getPassword());
            if (user.getPassword().equals(userVo.getOldPassword())) {
                user.setPassword(userVo.getNewPassword());
                if (0 < userMapper.updatePassWordByUserSystemId(user)) {
                    rtv = true;
                }
            } else {
                throw new EdiException("请确认旧密码是否正确!");
            }
        } else {
            throw new EdiException("用户不存在,请重新登录!");
        }
        return rtv;
    }

    @Override
    public boolean register(User user) throws EdiException {
        boolean rtv = false;
        if (null != user) {
            user.setIdNumber("'"+user.getIdNumber()+"'");
            user.setSex("'"+user.getSex()+"'");
            int success = userMapper.insertNewUser(user);
            if (0 < success) {
                rtv = true;
            }
        } else {
            throw new EdiException("请输入注册信息!");
        }
        return rtv;
    }

}
